﻿Public Class frmcategory
    Dim Strsql As String
    Dim Mysql1 As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Function Load_Data(ByVal siteid As Integer, ByVal sectionid As Integer) As Boolean
        Try
            Load_Data = False
            Dim DS As New DataSet
            Strsql = "select category_id,category_name,note,active,sectionid from category where siteid = " & siteid & " and sectionid = " & sectionid & " order by active desc ,category_name"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_category", "category")
            Debug.Print(Strsql)
            Pn_Framework.Set_GridSetting(Me.gridcategory, DS)
            Me.gridcategory.Columns.Item(4).Visible = False
            Load_Data = True
            On_Load = False
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message())
        End Try

    End Function
    Function Load_ComboboxData_section(ByVal siteid As Integer) As Boolean
        Try
            Dim DS As New DataSet
            Load_ComboboxData_section = False
            Strsql = "select * from section where active = 1 and siteid = " & siteid & " order by section_name "
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_section", "section")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSection
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "section_name"
                .ValueMember = "section_id"
            End With
            Load_ComboboxData_section = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_section - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function
    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
    Private Sub frmcategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_ComboboxData_site()
        Load_ComboboxData_section(Me.CboSite.SelectedValue)
        If My.Settings.CurrentuserGroup <> 1 Then Me.CboSite.Enabled = False
        If Load_Data(Me.CboSite.SelectedValue, Me.CboSection.SelectedValue) = False Then
            MsgBox("ระบบพบว่าฐานข้อมูลของคุณไม่ update หรือ ไม่ถูกต้อง", MsgBoxStyle.Exclamation, "กรุณาตรวจสอบฐานข้อมูลของคุณ")
            Exit Sub
        End If
    End Sub

    Private Sub gridcategory_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridcategory.CellClick
        Try
            With gridcategory
                If .RowCount = 0 Then Exit Sub
                Me.Txtid.Text = .Item(0, e.RowIndex).Value.ToString
                Me.Txtname.Text = .Item(1, e.RowIndex).Value.ToString
                Me.TxtNote.Text = .Item(2, e.RowIndex).Value.ToString
                Me.ChkActive.Checked = .Item(3, e.RowIndex).Value.ToString
                Me.CboSection.SelectedValue = .Item(4, e.RowIndex).Value.ToString

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub gridcategory_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridcategory.CellContentClick

    End Sub


    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        Me.Txtid.Text = ""
        Me.Txtname.Text = ""
        Me.TxtNote.Text = ""
        Me.ChkActive.Checked = False
    End Sub


    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If Me.Txtname.Text.Trim = "" Then Exit Sub
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            Dim DS As New DataSet
            If Me.Txtid.Text = "" Then
                Strsql = "Select * from category where category_name= '" & Me.Txtname.Text & "' and siteid = " & Me.CboSite.SelectedValue & " and sectionid = " & Me.CboSection.SelectedValue
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Category", "Category")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("Category นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into category (category_name,note,siteid,sectionid) values ('" & Me.Txtname.Text & "', '" & Me.TxtNote.Text & "'," & Me.CboSite.SelectedValue & "," & Me.CboSection.SelectedValue & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update category set category_name= '" & Me.Txtname.Text & "', note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked & " , sectionid = " & Me.CboSection.SelectedValue
                Strsql = Strsql & " where category_id = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            End If
            'Refresh Data
            Load_Data(Me.CboSite.SelectedValue, Me.CboSection.SelectedValue)
            BtnNew_Click(sender, e)

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub
            Strsql = "Delete from category where category_id = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql1.MySQLExecute(Strsql)
            'Refresh Data
            Load_Data(Me.CboSite.SelectedValue, Me.CboSection.SelectedValue)
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message())
        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub CboSite_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSite.SelectedIndexChanged
        If On_Load = False Then
            Load_ComboboxData_section(Me.CboSite.SelectedValue)
            Load_Data(Me.CboSite.SelectedValue, Me.CboSection.SelectedValue)
            Me.Txtid.ReadOnly = True
            Me.BtnNew_Click(sender, e)
        End If

    End Sub

    Private Sub CboSection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSection.SelectedIndexChanged
        If On_Load = False Then Load_Data(Me.CboSite.SelectedValue, Me.CboSection.SelectedValue)
    End Sub
End Class